<?php
include_once dirname(__FILE__).'/_config.php';

if(!empty($_POST['formAction'])){
	$__arr = explode('.', $_POST['formAction']);
	if(isset($__arr[1])){
		$__frm = $__arr[0];
		$__fact = $__arr[1];
	}else{
		$__fact = $__arr[0];
	}
	$__arr = explode('_', $__fact);
	$__mdl = $__arr[0];
	$__do = $__arr[1]; // 动作类型
}

$__ajax = empty($_REQUEST['ajax']) ? '' : trim($_REQUEST['ajax']);
//if(!empty($__mdl) && empty($__do))$__do = 'list';	// 默认是列表
$__path = str_replace('.', '/', $__frm);
$__file = dirname(__FILE__).(str_replace(array('//', '..'), array('/', '.'), "/{$__path}/{$__mdl}.{$__do}.php"));


$__tpl = array(
	'file'		=> '_i.html',
	'header'	=> $__frm == 'ajax' ? '' : '_header.tpl',
	'footer'	=> $__frm == 'ajax' ? '' : '_footer.tpl',
	'path'		=> empty($__path) ? '' : "{$__path}/",
	'body'		=> trim(str_replace(array('//', '..'), array('/', '.'), "{$__mdl}.{$__do}.html"), '/'),
	'formAction'=> "{$__mdl}_{$__do}",
);

switch($__frm){
	case 'user':		// 如果是用户中心，需要判断是否登录
		islogin();
		break;
	case 'do':			// 如果有动作，需要判断是否登录
		islogin();
		break;
}

#$__l = ea_read_language($__f_lang_setting, "gm_{$__mdl}");

// 查看源代码
if((defined('DEBUG_PR') && DEBUG_PR == true) || (defined('DEBUG_FB') && DEBUG_FB == true)){
	// 调试类
	##@include_once CLS_PTH.'tools/Trace.class.php';
	if(isset($_GET['source']) && $_GET['source'] == 'yes'){
		highlight_file($__file);
		exit();
	}
}

if(file_exists($__file)){
	include_once $__file;
}else{
	$__tpl_file = $__tpl_pth.$__tpl['path'].$__tpl['body'];
	if(!file_exists($__tpl_file)){
		exit("Template file {$__tpl_file} not exist.");
	}
}

// 执行 action 插件
if(defined('PLUGIN_OPEN') && PLUGIN_OPEN == true){
	$GLOBALS['ars'] = array();
	$actions = has_action($__theme_pth.$__tpl['path'].$__tpl['body']);
	if($actions){
		apply_action($actions);
	}
	$vars['ars'] = $GLOBALS['ars'];
}

if(/* $__ajax == 'ajax' || */ $__frm == 'do'){
	exit;
}
include_once dirname(__FILE__).'/_flush.php';
?>